using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Splino.D3D
{
    public partial class SingleSelectionForm : Form
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public SingleSelectionForm()
        {
            InitializeComponent();
        }

        private void SingleSelectionForm_Deactivate(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SingleSelectionForm_Load(object sender, EventArgs e)
        {
            Point formLocation = new Point(Cursor.Position.X + 10, Cursor.Position.Y + 10);
            if (formLocation.X + this.Width > Screen.FromControl(SplinoForm.Instance).WorkingArea.Width)
                formLocation = new Point(
                    formLocation.X - ((formLocation.X + this.Width) -
                    Screen.FromControl(SplinoForm.Instance).WorkingArea.Width),
                    formLocation.Y);
            if (formLocation.Y + this.Height > Screen.FromControl(SplinoForm.Instance).WorkingArea.Height)
                formLocation = new Point(
                    formLocation.X,
                    formLocation.Y - ((formLocation.Y + this.Height) -
                    Screen.FromControl(SplinoForm.Instance).WorkingArea.Height));

            Location = formLocation;

            foreach (SplinoObject splinoObject in SceneManager.Instance.SelectedObjects)
            {
                ListViewItem listViewItem = new ListViewItem(splinoObject.Name);
                listViewItem.Tag = splinoObject;
                ListViewItem.ListViewSubItem typeSubItem = 
                    new ListViewItem.ListViewSubItem(listViewItem, splinoObject.ObjectType);
                listViewItem.SubItems.Add(typeSubItem);
                _listView.Items.Add(listViewItem);
            }
        }

        private void _listView_MouseMove(object sender, MouseEventArgs e)
        {
            ListViewItem listViewItem = _listView.GetItemAt(e.X, e.Y);

            if (_listView.SelectedItems.Count != 0 && _listView.SelectedItems[0] != listViewItem)
            {
                List<SplinoObject> clearList = new List<SplinoObject>();
                foreach (SplinoObject splinoObject in SceneManager.Instance.SelectedObjects)
                    clearList.Add(splinoObject);
                foreach (SplinoObject splinoObject in clearList)
                    splinoObject.Deselect();
                SceneManager.Instance.SelectedObjects.Clear();
            }

            if (listViewItem != null)
            {
                if (listViewItem.Selected == false)
                {
                    switch (listViewItem.Text)
                    {
                        case "All":
                            foreach (SplinoObject splinoObject in SceneManager.Instance.SelectedObjects)
                                splinoObject.Select();
                            listViewItem.Selected = true;
                            break;
                        case "None":
                            listViewItem.Selected = true;
                            break;
                        default:
                            listViewItem.Selected = true;
                            SplinoObject newSplinoObject = (SplinoObject)listViewItem.Tag;
                            newSplinoObject.Select();
                            break;
                    }
                }
            }
            else
                foreach (ListViewItem selectedListViewItem in _listView.SelectedItems)
                    selectedListViewItem.Selected = false;
        }

        private void _listView_MouseUp(object sender, MouseEventArgs e)
        {
            this.Close();
        }

    }
}